//
//  BaseAlertWindow.m
//  DriftDog
//
//  Created by rimi on 15/9/10.
//  Copyright (c) 2015年 廖马林. All rights reserved.
//

#import "BaseAlertWindow.h"

#define SCREEN_W CGRectGetWidth([[UIScreen mainScreen] bounds])
#define SCREEN_H CGRectGetHeight([[UIScreen mainScreen] bounds])

@interface BaseAlertWindow ()
{
    UIWindow *_popWindow;
}

@end

@implementation BaseAlertWindow

#pragma mark - init

- (instancetype)init {

    if (self = [super init]) {
        
        self.frame = CGRectMake(0, 0, SCREEN_W * 0.75, SCREEN_H * 0.35);
        [self initializeUserInterface];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {

    if (self = [super initWithFrame:frame]) {
        
        self.frame = frame;
        [self initializeUserInterface];
    }
    return self;
}

- (void)initializeUserInterface {

    /* 初始化窗口视图 */
    _popWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    _popWindow.backgroundColor = [UIColor colorWithWhite:0 alpha:0.2];
    _popWindow.windowLevel = UIWindowLevelAlert;
    
    /* 初始化视图 */
    self.center = CGPointMake(SCREEN_W * 0.5, SCREEN_H * 0.5);
    self.layer.cornerRadius = 5;
    self.layer.masksToBounds = YES;
    self.backgroundColor = [UIColor whiteColor];
    
    [_popWindow addSubview:self];
}

#pragma mark - interface methods

- (void) show {
    
    _popWindow.hidden = NO;
    [_popWindow makeKeyAndVisible];
}

- (void)hidden {
// #warning resignKeyWindow是什么意思? 是注销的意思
    [self removeFromSuperview];
    _popWindow.alpha = 0;
    _popWindow.hidden = YES;
    [_popWindow resignKeyWindow];
}

@end
